using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors.DXErrorProvider;

namespace Comteck.Winforms.Validations {
  /// <summary>
  /// devexpress 验证帮助类
  /// </summary>
  public static class ValidationRuleHelper {
    /// <summary>
    /// 
    /// </summary>
    /// <param name="ruleList"></param>
    /// <returns></returns>
    public static ValidationRule GetRule(IEnumerable<MvcValidationRule> ruleList) {
      if (ruleList.IsNullOrEmpty()) {
        return null;
      }

      // 只有一个
      if (ruleList.Count() == 1) {
        var first = ruleList.First();

        // 必须输入，使用系统内置的校验方法
        if (first.ValidationType == MvcValidationType.RequiredAttribute) {
          return NotEmpty(ruleList.First().ErrorMessage);
        }
      }

      return Multi(ruleList);
    }

    /// <summary>
    /// 判断控件是否为空
    /// </summary>
    /// <param name="errorMessage"></param>
    /// <returns></returns>
    public static ConditionValidationRule NotEmpty(string errorMessage) {
      return new ConditionValidationRule() {
        ConditionOperator = ConditionOperator.IsNotBlank,
        ErrorText = errorMessage,
        ErrorType = ErrorType.Critical
      };
    }

    /// <summary>
    /// 判断是否大于等于某个数
    /// </summary>
    /// <param name="errorMessage"></param>
    /// <param name="number">数值</param>
    public static ConditionValidationRule NoLessNumber(string errorMessage, decimal number) {
      return new ConditionValidationRule {
        ConditionOperator = ConditionOperator.GreaterOrEqual,
        ErrorText = errorMessage,
        Value1 = number
      };
    }

    /// <summary>
    /// 判断是否大于某个数
    /// </summary>
    /// <param name="number">数值</param>
    public static ConditionValidationRule GreaterNumber(string errorMessage, decimal number) {
      var rule = new ConditionValidationRule {
        ConditionOperator = ConditionOperator.Greater,
        ErrorText = string.Format("该值必须大于{0}！", number),
        Value1 = number
      };
      return rule;
    }

    /// <summary>
    /// 判断是否在指定数（包含）之间
    /// </summary>
    /// <param name="small">小数</param>
    /// <param name="big">大数</param>
    /// <returns></returns>
    public static ConditionValidationRule BetweenNumbers(decimal small, decimal big) {
      var rule = new ConditionValidationRule {
        ConditionOperator = ConditionOperator.Between,
        ErrorText = string.Format("该值不能小于{0}且不能大于{1}！", small, big),
        Value1 = small,
        Value2 = big
      };
      return rule;
    }

    /// <summary>
    /// 判断结束时间是否不小于指定时间
    /// </summary>
    /// <param name="startTime">指定时间</param>
    public static ConditionValidationRule NoLessDate(DateTime startTime) {
      var rule = new ConditionValidationRule {
        ErrorText = string.Format("结束时间不能小于开始时间！"),
        ConditionOperator = ConditionOperator.GreaterOrEqual,
        Value1 = startTime
      };
      return rule;
    }


    /// <summary>
    /// 多个匹配规则
    /// </summary>
    /// <param name="ruleList">是否允许为空</param>
    public static MultiValidationRule Multi(IEnumerable<MvcValidationRule> ruleList) {
      return new MultiValidationRule(ruleList);
    }

  }

  public class StringLengthValidation : ConditionValidationRule {
    //是否允许空值
    public bool allowNullOrEmpty { get; set; }
    public override bool Validate(Control control, object value) {
      return (int)this.Value1 == ((string)value)?.Length || (this.allowNullOrEmpty && ((string)value).IsNullOrEmpty());
    }
  }
}
